package net.huashitong.ssydt.provider.h5.currentpolitics.service;


import net.huashitong.ssydt.exam.entity.vo.BaseQuestion;
import net.huashitong.ssydt.pk.entity.SsPkYmdxQuestionRelation;
import net.huashitong.ssydt.provider.exam.service.ExamService;
import net.huashitong.ssydt.provider.h5.currentpolitics.dto.WxSdkDTO;
import net.huashitong.ssydt.provider.h5.currentpolitics.utils.SignUtil;
import net.huashitong.ssydt.provider.pk.service.SsPkYmdxQuestionRelationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.12.27
 */
@Service
@Transactional
public class CurrentPoliticsServiceImpl implements CurrentPoliticsService {

    private Logger logger = LoggerFactory.getLogger(CurrentPoliticsService.class);

    private ExamService examService;

    private SsPkYmdxQuestionRelationService ssPkYmdxQuestionRelationService;

    private WxService wxService;

    @Autowired
    public CurrentPoliticsServiceImpl(ExamService examService,
                                      SsPkYmdxQuestionRelationService ssPkYmdxQuestionRelationService,
                                      WxService wxService) {
        this.examService = examService;
        this.ssPkYmdxQuestionRelationService = ssPkYmdxQuestionRelationService;
        this.wxService = wxService;
    }

    @Override
    public List<BaseQuestion> getQuestions() {
        List<SsPkYmdxQuestionRelation> relations = ssPkYmdxQuestionRelationService.findNewest();
        List<BaseQuestion> list = new ArrayList<>();
        for (SsPkYmdxQuestionRelation relation : relations) {
            BaseQuestion question = examService.getBaseQuestion(relation.getQuestionId());
            list.add(question);
        }
        return list;
    }


    @Override
    public WxSdkDTO getWxSdk() throws IOException {
        String jsapiTicket = wxService.getJsapiTicket().getTicket();
        Map<String, String> map = SignUtil.sign(jsapiTicket, wxService.getUrl());
        logger.info("jsapiTicket:[{}],url:[{}]", jsapiTicket, wxService.getUrl());
        return new WxSdkDTO(map, wxService.getAppId(), wxService.getBaseUrl());
    }
}
